package speech.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

import speech.tag.SpeechActTagger;

public class Parser {
	private final int SPLIT_TEST_FILE_PERCENTAGE = 10;
	
	/**
	 * Index a file that contains sequences of POS tokens.
	 * 
	 * @param dictionary
	 * @param filepath
	 */
	private void indexPOSFile(ArrayList<String> dictionary, File posFile) {
		try {
			Scanner s = new Scanner(new BufferedReader(new FileReader(posFile)));
			
			while (s.hasNext()) {
				String token = s.next();
				
				if (!dictionary.contains(token)) {
					dictionary.add(token);
				}
			}
			
			s.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Construct dictionary from POS files.
	 * 
	 * @param contentsFile
	 * @return
	 */
	public ArrayList<String> constructDictionaryFromPOSFiles(String posFolderPath) {
		ArrayList<String> dictionary = new ArrayList<String>();
		
		File posFolder = new File(posFolderPath);
		for (File file : posFolder.listFiles()) {
			indexPOSFile(dictionary, file);
		}
		
		return dictionary;
	}
	
	/**
	 * Encode a file of tokens using the given dictionary by generating a sequence file.
	 * 
	 * @param dictionary
	 * @param inFilepath
	 * @param outFilepath
	 */
	public int encodePOSFile(ArrayList<String> dictionary, String inFilepath, String outFilepath) {
		int wordCnt = 0;
		int uttCnt = 0;
		
		try {
			String line;
			
			BufferedReader input = new BufferedReader(new FileReader(inFilepath));
			PrintWriter output = new PrintWriter(new FileWriter(outFilepath));
			
			while ((line = input.readLine()) != null) {
				StringTokenizer s = new StringTokenizer(line);
				if (s.countTokens() < 2) continue;
				while (s.hasMoreTokens()) {
					int index = dictionary.indexOf(s.nextToken());
					if (index != -1) {
						output.print("" + index + " ; ");
						wordCnt ++;
					}
				}
				output.println();
				uttCnt ++;
			}
			
			output.close();
			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return (uttCnt == 0) ? 0 : wordCnt / uttCnt;
	}
	
	public void splitUttFile(String filename, String trainFilePath, String testFilePath) {
		try {
			BufferedReader bf = new BufferedReader(new FileReader(filename));
			
			String l;
			int counter = 0;
			ArrayList<String> lines = new ArrayList<String>();
			while ((l = bf.readLine()) != null) {
				lines.add(l);
				counter ++;
			}
			bf.close();
			
			int threshold = counter / SPLIT_TEST_FILE_PERCENTAGE;
			
			PrintWriter test = new PrintWriter(new FileWriter(testFilePath));
			PrintWriter train = new PrintWriter(new FileWriter(trainFilePath));
			for (int i = 0; i < counter; ++ i) {
				if (i < threshold) test.println(lines.get(i));
				else train.println(lines.get(i));
			}
			test.close();
			train.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void processRawTagTextFiles(String folder) throws FileNotFoundException {
		PrintWriter pw = new PrintWriter(SpeechActTagger.INPUT_FILE);
		
		File myFolder = new File(folder);
		for (File f : myFolder.listFiles()) {
			String filepath = f.getPath();
			String testFilePath = "text/test" + filepath.substring(filepath.lastIndexOf('\\') + 1);
			String trainFilePath = "text/train" + filepath.substring(filepath.lastIndexOf('\\') + 1);
//			System.out.println(filepath + " " + trainFilePath + " " + testFilePath);
			splitUttFile(filepath, trainFilePath, testFilePath);
			
			String tag;
			if (filepath.lastIndexOf('.') != -1) {
				tag = filepath.substring(filepath.lastIndexOf('\\') + 1, filepath.lastIndexOf('.'));
			}
			else {
				tag = filepath.substring(filepath.lastIndexOf('\\') + 1);
			}
			
			pw.println(tag + " " + trainFilePath.substring(trainFilePath.lastIndexOf('/') + 1)
					+ " " + testFilePath.substring(testFilePath.lastIndexOf('/') + 1));
		}
		
		pw.close();
	}
	
	public void parseDialogue(String filepath) {
		BufferedReader bf;
		PrintWriter pw;
		
		try {
			bf = new BufferedReader(new FileReader(filepath));
			pw = new PrintWriter(new BufferedWriter(new FileWriter("parsed" + filepath)));
			String line;
			
			while ((line = bf.readLine()) != null) {
				int index = line.indexOf(':');
				if (index != -1) line = line.substring(index + 2);
				line = line.toLowerCase();
				line = line.replaceAll("\\. ", ".\n");
				line = line.replaceAll("\\? ", "?\n");
				line = line.replaceAll("\\! ", "!\n");
				line = line.replaceAll("\\.\\.\\.", ".\n");
				line = line.replaceAll("\\?\\?\\?", "?\n");
				line = line.replaceAll("\\!\\!\\!", "!\n");

				String[] newLines = line.split("\n");
				
				for (String nl : newLines) {
					if (nl.compareTo("") != 0) {
						nl = "id\t\t" + nl;
						pw.println(nl);
					}
				}
			}
			
			pw.close();
			bf.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
